package com.zpark.drug.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpark.drug.common.ResultMapUtil;
import com.zpark.drug.pojo.Employee;
import com.zpark.drug.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 转向供应商页面
     * @return
     */
    @RequestMapping
    public String login(){
        return "/employee";
    }
    /**
     * 转向供应商新增页面
     * @return
     */
    @RequestMapping(value = "/employeePage")
    public String employeePage(){
        return "/employeePage";
    }
    /**
     * 转向供应商编辑页面
     * @return
     */
    @RequestMapping(value = "/employeeQueryById")
    public String employeeQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        Employee employee = employeeService.findById(id);
        model.addAttribute("obj",employee);
        return "/employeePage";
    }
    /**
     * 获取所有供应商
     * @return
     */
    @RequestMapping(value = "/employeeList")
    @ResponseBody
    public Object employeeList(){
        List<Employee> employeeList = employeeService.findAll();
        return ResultMapUtil.getHashMapList(employeeList);
    }

    /**
     * 添加一个供应商
     * @return
     */
    @RequestMapping(value = "/employeeAdd")
    @ResponseBody
    public Object employeeAdd(Employee employee){
        try {
            employee.setBirthday(new Date());
            employee.setEntry(new Date());
            int i = employeeService.addEmployee(employee);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/employeeEdit")
    @ResponseBody
    public Object employeeEdit(Employee employee){
        try {
            int i = employeeService.updateEmployee(employee);

            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个供应商
     * @return
     */
    @RequestMapping(value = "/employeeDelById")
    @ResponseBody
    public Object employeeDelById(Integer id){
        try {
            int i = employeeService.deleteEmployeeById(id);

            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }
    /**
     * 分页查询供应商列表
     * @return
     */
    @RequestMapping(value = "/employeeQueryPage")
    @ResponseBody
    public Map<String, Object> employeeQueryPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String param) {
        PageInfo<Employee> employeePageInfo = employeeService.findPage(page, limit, param);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", employeePageInfo.getTotal());
        map.put("data", employeePageInfo.getList());
        return map;
    }

}
